<?php
// *** Tangra (Application Framework and Tools for PHP)
// $Id$
//


// Boot script. It is always executed first in every page.
// Brings up basic functionality

//execution of the script must finish normally, even if user aborts
ignore_user_abort(true);

if (!defined('TANGRA_SITE_BOOT_DIR')) define('TANGRA_SITE_BOOT_DIR', dirname(__FILE__).'/');


if (file_exists(TANGRA_SITE_BOOT_DIR.'boot_conf.inc.php')) {
	require_once(TANGRA_SITE_BOOT_DIR.'boot_conf.inc.php');


	if (!defined('TANGRA_SITE_BOOT_TANGRA_LIB_PATH')) {
		define('TANGRA_SITE_BOOT_TANGRA_LIB_PATH', TANGRA_SITE_BOOT_DIR.'../ext_inc/tangra2/tangra_lib/');
	}



	if (!defined('TANGRA_SITE_BOOT_CLASSES_PATH')) {
		define('TANGRA_SITE_BOOT_CLASSES_PATH', TANGRA_SITE_HIDDEN_PATH.'boot/');
	}

	if (!defined('TANGRA_SITE_BOOT_CONFIG_FILES_PATH')) {
		define('TANGRA_SITE_BOOT_CONFIG_FILES_PATH', TANGRA_SITE_HIDDEN_PATH.'conf/');
	}
} else {
	define('TANGRA_SITE_BOOT_TANGRA_LIB_PATH', TANGRA_SITE_BOOT_DIR.'../ext_inc/tangra2/tangra_lib/');
	define('TANGRA_SITE_BOOT_CLASSES_PATH', TANGRA_SITE_BOOT_DIR.'../hidden/boot/');
	define('TANGRA_SITE_BOOT_CONFIG_FILES_PATH', TANGRA_SITE_BOOT_DIR.'../hidden/conf/');
}



// getting tangra lib
if (file_exists(TANGRA_SITE_BOOT_TANGRA_LIB_PATH.'lib/tangra2.inc.php')) {
	// trying configured (or default value)
	require_once(TANGRA_SITE_BOOT_TANGRA_LIB_PATH.'lib/tangra2.inc.php');
} else {
	// trying include_path
	require_once('lib/tangra2.inc.php');
}


// if we are executed in "cli" mode - use default exception print
if (php_sapi_name() != 'cli') {
	// Setting nicer version of exception printing
	set_exception_handler('exception_html_print');
}

// setting error handler assures that user will not see ugly error messages.
// Default error handler (tangra_error_handler) will capture the error and throw exception (and
// the exception will be handled in appropriate way depending on MS_SITE_MODE setting in machine_specific.conf
set_error_handler('tangra_error_handler');


require_once(TANGRA_MAIN_DIR.'exceptions/tangra_user_exception.class.php');


require_once(TANGRA_SITE_BOOT_CLASSES_PATH.'directory_structure_base_config.class.php');
require_once(TANGRA_SITE_BOOT_CLASSES_PATH.'directory_structure_base_config_loader_file.class.php');

$PATHS = new Directory_Structure_Base_Config();

if (php_sapi_name() == 'cli') {
	define('TANGRA_MACHINE_SPECIFIC_CONF_FILENAME', 'machine_specific.cli.conf');
} else {
	define('TANGRA_MACHINE_SPECIFIC_CONF_FILENAME', 'machine_specific.conf');
}


$config_loader = new Directory_Structure_Base_Config_Loader_File(TANGRA_SITE_BOOT_CONFIG_FILES_PATH.'/'.TANGRA_MACHINE_SPECIFIC_CONF_FILENAME);
$config_loader->load_config($PATHS);


// getting server time zone
$tz = $config_loader->get_conf_value('MS_SERVER_TIME_ZONE', false);
date_default_timezone_set($tz ? $tz : 'Europe/Sofia');

if (function_exists('mb_internal_encoding')) {
	$default_encoding = $config_loader->get_conf_value('MS_DEFAULT_ENCODING', false);
	mb_internal_encoding($default_encoding ? $default_encoding : 'utf-8');
}


// "Cure" for magic quotes = On in shared hostings (and other cases where you can't change php.ini)
// Result of this code is like magic quotes was always = Off
require_once(TANGRA_MAIN_DIR.'misc/misc.inc.php');
if((function_exists("get_magic_quotes_gpc") && get_magic_quotes_gpc())
		|| (ini_get('magic_quotes_sybase') && (strtolower(ini_get('magic_quotes_sybase'))!="off")) ) {
    $_GET = stripslashes_deep($_GET);
    $_POST = stripslashes_deep($_POST);
    $_COOKIE = stripslashes_deep($_COOKIE);
}



// Creating modules registar. That way modules MAY store their configuration in it.
require_once(TANGRA_MAIN_DIR.'core/modules_register.class.php');
$_MODULES_REGISTER = new Modules_Register();


require_once(TANGRA_MAIN_DIR.'core/rc_d.inc.php');

function boot($level) {
	global $PATHS;

	$rc_d_scripts_ordered = get_rc_d_scripts_ordered($PATHS->get_hidden()."/boot/boot_$level");
	try {
		foreach($rc_d_scripts_ordered as $module_boot_script) {
			require_once($module_boot_script);
		}
	} catch(Exception $e) {
		error_log(date('Y-m-d H:i:s')."\r\n", 3, $PATHS->get_scratch().'logs/boot_errors.log');
		error_log($e->getMessage(), 3, $PATHS->get_scratch().'logs/boot_errors.log');
		error_log("\r\n", 3, $PATHS->get_scratch().'logs/boot_errors.log');
		error_log($e->getTraceAsString()."\r\n", 3, $PATHS->get_scratch().'logs/boot_errors.log');
		error_log("\r\n", 3, $PATHS->get_scratch().'logs/boot_errors.log');
		error_log("\r\n", 3, $PATHS->get_scratch().'logs/boot_errors.log');

		header('HTTP/1.1 500 Internal Server Error');
		print('<h1>Server error occured. Please try again later.</h1>');
		printbr('Something on the server broke. Please try again later.');
		printbr('We are very sorry about the inconvenience.');
		ob_flush();
		exit(1);
	}
}


